package cn.softgame.cms.secure.module.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.softgame.cms.common.exception.BissinessException;
import cn.softgame.cms.open.authentication.domain.SysModule;
import cn.softgame.cms.open.authentication.domain.SysRight;
import cn.softgame.cms.open.common.service.CommonService;
import cn.softgame.cms.secure.module.service.ModuleService;

@Controller
@RequestMapping("/secure/module/*")
public class ModuleController {
	
	private Logger logger = LoggerFactory.getLogger(ModuleController.class);
	
	@Autowired
	private ModuleService moduleService;
	
	@Autowired
	private CommonService commonService;
	
	@RequestMapping(value="listModules.html")
	public ModelAndView listModules() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("module", new SysModule());
		mav.setViewName("secure/module/module");
		return mav;
	}
	
	@RequestMapping("loadModulesData.html")
	public @ResponseBody List<SysModule> loadModulesData(){
		List<SysModule> list = moduleService.loadModulesData();
		// 去掉parentSysModule
		for(SysModule m : list){
			m.setParentSysModule(null);
		}
		return list;
	}
	
	@RequestMapping("queryModuleByModuleId.html")
	public ModelAndView queryModuleByModuleId(@RequestParam String moduleId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("module", moduleService.queryModuleByModuleId(moduleId));
		mav.setViewName("secure/module/moduleForm");
		return mav;
	}
	
	@RequestMapping("queryChildModuleByModuleId.html")
	public ModelAndView queryChildModuleByModuleId(@RequestParam String moduleId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("childModules", moduleService.queryChildModuleByModuleId(moduleId));
		mav.setViewName("secure/module/modal");
		return mav;
	}
	
	@RequestMapping("updateSeqNumBatch.html")
	public void updateSeqNumBatch(@RequestParam String moduleIds){
		moduleService.updateSeqNumBatch(moduleIds);
	}
	
	@RequestMapping("addRootModule.html")
	public ModelAndView addRootModule() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("module", new SysModule());
		mav.setViewName("secure/module/moduleForm");
		return mav;
	}
	
	@RequestMapping("addModule.html")
	public ModelAndView addModule(@RequestParam String parentModuleId) {
		ModelAndView mav = new ModelAndView();
		SysModule parentModule = moduleService.queryModuleByModuleId(parentModuleId);
		SysModule module = new SysModule();
		module.setParentSysModule(parentModule);
		mav.addObject("module", module);
		mav.setViewName("secure/module/moduleForm");
		return mav;
	}
	
	@RequestMapping(value="delete.html",produces="text/plain;charset=UTF-8")
	public @ResponseBody Map<String,Object> delete(@RequestParam String moduleId) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			moduleService.deleteModule(moduleId);
			map.put("flag", "success");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", "error");
			logger.error("cn.softgame.cms.secure.module.controller.ModuleController.delete", e);
		}
		return map;
	}
	
	/**
	 * 保存模块
	 * 注意:单独返回String有中文时,必须加上produces="text/plain;charset=UTF-8"处理中文乱码
	 * @param module
	 * @return
	 * @throws BissinessException 
	 */
	@RequestMapping(value="save.html")
	public @ResponseBody Map<String,Object> save(SysModule module) throws BissinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			if(module!=null){
				String moduleId = module.getModuleId();
				SysModule parent = module.getParentSysModule();
				String parentModuleId = "0";
				String parentTreeCode = null;
				if(parent!=null){
					parentModuleId = parent.getModuleId();
					parentTreeCode = parent.getTreeCode();
					
					if(parentModuleId==null || "".equals(parentModuleId)){
						parentModuleId = "0";
					}
				}
				
				module.setParentModuleId(parentModuleId);
				// 不管是新增还是更新,都生成一个TreeCode,简化逻辑
				module.setTreeCode(commonService.getMaxTreeCode("Sys_Module", "tree_code", null, parentTreeCode));
				if(moduleId!=null && !"".equals(moduleId)){
					moduleService.update(module);
				}else{
					SysRight sr = new SysRight();
					sr.setRightId(commonService.getCode());
					sr.setRightCode(module.getRightCode());
					sr.setTreeCode(commonService.getMaxTreeCode("Sys_Right", "tree_code", null,null));
					sr.setRightName(module.getModuleName()+"权限");
					module.setRightId(sr.getRightId());
					module.setModuleId(commonService.getCode());
					moduleService.save(module,sr);
				}
			}
			map.put("flag", "success");
			map.put("module", module);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", "error");
			logger.error("cn.softgame.cms.secure.module.controller.ModuleController.save", e);
		}
		return map;			
	}

	/**
	 * 检查权限编码是否存在
	 * @param rightCode
	 * @return
	 */
	@RequestMapping(value="checkRightCodeIsExits.html")
	public @ResponseBody boolean checkRightCodeIsExits(@RequestParam String rightCode){
		return moduleService.checkRightCodeIsExits(rightCode);
	}
}
